import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt
import chardet
import folium as fm
from folium import Marker, GeoJson
from folium.plugins import MarkerCluster, HeatMap, StripePattern
import geopandas as gpd
from geopandas import GeoSeries
from shapely.geometry import Point, LineString
import branca as br
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt
import chardet
import pandas as pd
import numpy as np
import folium
from folium.plugins import BeautifyIcon
import branca
import folium
from folium.plugins import MarkerCluster
from folium import IFrame
import shapely
from shapely.geometry import Point
import pandas as pd
import geopandas as gpd
from geopandas.tools import sjoin
import base64
import math
import os
import numpy as np
import seaborn as sns
import unicodedata
import pysal as ps
import branca
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
import pandas as pd
import numpy as np
import os
import time
import re
import unidecode
import time
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager
import re
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_12984\208844336.py in <module>
4 import matplotlib.pyplot as plt
5 import chardet
----> 6 import folium as fm
7 from folium import Marker, GeoJson
8 from folium.plugins import MarkerCluster, HeatMap, StripePattern
ModuleNotFoundError: No module named 'folium'
# import csv file of public health centers: MINSA, ESSALUD and Regional Goverment
# coordinates information (NORTE: longitud) (ESTE: latitud)
health_centers = pd.read_csv(r'../_data/centros_hospitalarios.csv')
Public_health = gpd.GeoDataFrame( health_centers, geometry=gpd.points_from_xy( health_centers.NORTE, health_centers.ESTE ) )
Public_health = Public_health.set_crs( "EPSG:32718", allow_override = True)
Public_health.crs
# Keep only lima
hc2 = Public_health[Public_health.Departamento == 'LIMA'].copy()
hc3 = hc2[hc2.ESTE.notnull()].copy()
# Get lat and lon
hc3['lon'] = hc3.geometry.x
hc3['lat'] = hc3.geometry.y
# Import shapefile
geo_distrito = gpd.read_file( r'../_data/shape_file/DISTRITOS.shp')
# keep only Lima
geo2 = geo_distrito[geo_distrito.DEPARTAMEN == 'LIMA']
Health Centers in Lima
# FDef
zoom_start = 8
lat_hos = hc3["lat"].mean()
long_hos = hc3["lon"].mean()
centroid_lat = lat_hos
centroid_lon = long_hos
m = folium.Map([centroid_lat, centroid_lon], zoom_start=zoom_start)
for _, row in hc3.iterrows():
folium.Circle([row['lat'], row['lon']],
radius=50, color = "red",
fill_color="red", # divvy color
).add_to(m)
style2 = {'fillColor': '#ffffff', 'lineColor': '#00FFFFFF'}
folium.GeoJson(data=geo2["geometry"], style_function=lambda x:style2, name = 'district_border').add_to(m)
m
Make this Notebook Trusted to load map: File -> Trust Notebook
Make Clusters of different types of Health Establishments
# Get groups of latitude and longitude
hc3_sin = hc3[hc3.Tipo == 'ESTABLECIMIENTO DE SALUD SIN INTERNAMIENTO' ].copy()
hc3_con = hc3[hc3.Tipo == 'ESTABLECIMIENTO DE SALUD CON INTERNAMIENTO' ].copy()
hc3_apoyo = hc3[hc3.Tipo == 'SERVICIO MÉDICO DE APOYO' ].copy()
# Create tuple of lat and long
locations_sin = list( zip( hc3_sin["lat"], hc3_sin["lon"] ) )
locations_con = list( zip( hc3_con["lat"], hc3_con["lon"]))
locations_apoyo = list(zip(hc3_apoyo["lat"], hc3_apoyo["lon"]))
# Get lat and long health_center
zoom_start = 8
lat_health_center = hc3["lat"].mean()
long_health_center = hc3["lon"].mean()
centroid_lat = lat_health_center
centroid_lon = long_health_center
m = folium.Map([centroid_lat, centroid_lon], zoom_start=zoom_start)
style2 = {'fillColor': '#ffffff', 'lineColor': '#00FFFFFF'}
folium.GeoJson(data=geo2["geometry"], style_function=lambda x:style2, name = 'District Border').add_to(m)
### WITHOUT INTERNMENT
marker_cluster_1 = MarkerCluster(
locations=locations_sin,
name="ESTABLECIMIENTO DE SALUD SIN INTERNAMIENTO",
overlay=True,
control=True,
)
# WITH INTERNMENT
marker_cluster_2 = MarkerCluster(
locations=locations_con,
name="ESTABLECIMIENTO DE SALUD CON INTERNAMIENTO",
overlay=True,
control=True,
)
### SUPPORT
marker_cluster_3 = MarkerCluster(
locations=locations_apoyo,
name="SERVICIO MÉDICO DE APOYO",
overlay=True,
control=True,
)
marker_cluster_1.add_to(m)
marker_cluster_2.add_to(m)
marker_cluster_3.add_to(m)
# Add TileLayer
folium.TileLayer( tiles = 'https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png',
attr = '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
name = 'Stadia_AlidadeSmoothDark').add_to(m)
# folium.TileLayer('openstreetmap').add_to(m)
# folium.TileLayer('Stamen Terrain').add_to(m)
folium.LayerControl().add_to(m)
m
Make this Notebook Trusted to load map: File -> Trust Notebook
Make plot of Clusters
# Get lat and long health_center
zoom_start = 8
lat_health_center = hc3["lat"].mean()
long_health_center = hc3["lon"].mean()
centroid_lat = lat_health_center
centroid_lon = long_health_center
m = folium.Map([centroid_lat, centroid_lon], zoom_start=zoom_start)
# Add District shapefiles
style2 = {'fillColor': '#ffffff', 'lineColor': '#00FFFFFF'}
folium.GeoJson(data=geo2["geometry"], style_function=lambda x:style2, name = 'District Border').add_to(m)
# Genereate groups in folioum
health_centers_group = folium.FeatureGroup( name = 'Heakth Centers', control=True)
hth_con = folium.FeatureGroup( name = 'ESTABLECIMIENTO DE SALUD SIN INTERNAMIENTO', control = True )
hth_sin = folium.FeatureGroup( name = 'ESTABLECIMIENTO DE SALUD CON INTERNAMIENTO', control = True )
hth_apoyo = folium.FeatureGroup( name = 'SERVICIO MÉDICO DE APOYO', control = True )
# Helath establishment groups
# HEALTH ESTABLISHMENT WITHOUT INTERNMENT GROUP
for _, row in hc3_sin.iterrows():
# Customize pop up
html = row[ 'Nombre del establecimiento' ]
iframe = br.element.IFrame( html = html, width = 180, height = 100 )
popup = fm.Popup( iframe, parse_html = True )
folium.Marker( [row[ 'lat' ], row[ 'lon' ] ] ,
popup = popup,
icon = folium.Icon( color = "green" )
).add_to( hth_con )
# HEALTH ESTABLISHMENT WITH INTERNMENT Group
for _, row in hc3_con.iterrows():
# Customize pop up
html = row[ 'Nombre del establecimiento' ]
iframe = br.element.IFrame( html = html, width = 180, height = 100 )
popup = fm.Popup( iframe, parse_html = True )
folium.Marker( [row[ 'lat' ], row[ 'lon' ] ] ,
popup = row[ 'Nombre del establecimiento' ].lower(),
icon = folium.Icon( color = "blue" )
).add_to( hth_sin )
# Apoyo group
for _, row in hc3_apoyo.iterrows():
# Customize pop up
html = row[ 'Nombre del establecimiento' ]
iframe = br.element.IFrame( html = html, width = 180, height = 100 )
popup = fm.Popup( iframe, parse_html = True )
folium.Marker( [row[ 'lat' ], row[ 'lon' ] ] ,
popup = popup,
icon = folium.Icon( color = "red" )
).add_to( hth_apoyo )
# Add groups
m.add_child( hth_con )
m.add_child( hth_sin )
m.add_child( hth_apoyo )
legend_html_0 = '''
<div style="
position: fixed;
bottom: 10px;
left: 10px;
width: 200px;
height: 120px;
z-index:9999;
font-size:14px;
<p><a style="color:black;font-size:100%;margin-left:5px;">►</a> <b>Legend</b></p>
<p><a style="color:green;font-size:100%;margin-left:5px;">●</a> <b>Without Internment</b></p>
<p><a style="color:blue;font-size:100%;margin-left:5px;">●</a> <b>With Internment</b></p>
<p><a style="color:red;font-size:100%;margin-left:5px;">●</a> <b>Support</b></p>
</div>
<div style="
position: fixed;
bottom: 10px;
left: 10px;
width: 200px;
height: 120px;
z-index:9998;
font-size:14px;
background-color: #ffffff;
opacity: 0.7;
">
</div>
'''
# Add Legend
legend_html = "{% macro html(this, kwargs) %}" + legend_html_0 + "{% endmacro %}"
legend = branca.element.MacroElement()
legend._template = branca.element.Template(legend_html)
m.get_root().add_child(legend)
# Add layer
folium.LayerControl().add_to(m)
m
Make this Notebook Trusted to load map: File -> Trust Notebook
# Get lat and long health_center
zoom_start = 8
lat_health_center = hc3["lat"].mean()
long_health_center = hc3["lon"].mean()
centroid_lat = lat_health_center
centroid_lon = long_health_center
m = folium.Map([centroid_lat, centroid_lon], zoom_start=zoom_start)
# Add District shapefiles
style2 = {'fillColor': '#ffffff', 'lineColor': '#00FFFFFF'}
folium.GeoJson(data=geo2["geometry"], style_function=lambda x:style2, name = 'District Border').add_to(m)
# Genereate groups in folioum
health_centers_group = folium.FeatureGroup( name = 'Heakth Centers', control=True)
hth_con = folium.FeatureGroup( name = 'ESTABLECIMIENTO DE SALUD SIN INTERNAMIENTO', control = True )
hth_sin = folium.FeatureGroup( name = 'ESTABLECIMIENTO DE SALUD CON INTERNAMIENTO', control = True )
hth_apoyo = folium.FeatureGroup( name = 'SERVICIO MÉDICO DE APOYO', control = True )
# Helath establishment groups
# HEALTH ESTABLISHMENT WITHOUT INTERNMENT GROUP
for _, row in hc3_sin.iterrows():
folium.Circle( [row[ 'lat' ], row[ 'lon' ] ] ,
radius=15, color = "green",
fill_color="green", # divvy color
).add_to( hth_con )
# HEALTH ESTABLISHMENT WITH INTERNMENT Group
for _, row in hc3_con.iterrows():
folium.Circle( [row[ 'lat' ], row[ 'lon' ] ] ,
radius=15, color = "blue",
fill_color="blue", # divvy color
).add_to( hth_sin )
# Apoyo group
for _, row in hc3_apoyo.iterrows():
# Customize pop up
html = row[ 'Nombre del establecimiento' ]
iframe = br.element.IFrame( html = html, width = 180, height = 100 )
popup = fm.Popup( iframe, parse_html = True )
folium.Circle( [row[ 'lat' ], row[ 'lon' ] ] ,
radius=15, color = "red",
fill_color="red", # divvy color
).add_to( hth_apoyo )
# Add groups
m.add_child( hth_con )
m.add_child( hth_sin )
m.add_child( hth_apoyo )
legend_html_0 = '''
<div style="
position: fixed;
bottom: 10px;
left: 10px;
width: 200px;
height: 120px;
z-index:9999;
font-size:14px;
<p><a style="color:black;font-size:100%;margin-left:5px;">►</a> <b>Legend</b></p>
<p><a style="color:green;font-size:100%;margin-left:5px;">●</a> <b>Without Internment</b></p>
<p><a style="color:blue;font-size:100%;margin-left:5px;">●</a> <b>With Internment</b></p>
<p><a style="color:red;font-size:100%;margin-left:5px;">●</a> <b>Support</b></p>
</div>
<div style="
position: fixed;
bottom: 10px;
left: 10px;
width: 200px;
height: 120px;
z-index:9998;
font-size:14px;
background-color: #ffffff;
opacity: 0.7;
">
</div>
'''
# Add legend
legend_html = "{% macro html(this, kwargs) %}" + legend_html_0 + "{% endmacro %}"
legend = branca.element.MacroElement()
legend._template = branca.element.Template(legend_html)
m.get_root().add_child(legend)
# Add layer
folium.LayerControl().add_to(m)
m
Make this Notebook Trusted to load map: File -> Trust Notebook